﻿@using System.Reflection
@using AntDesign.TableModels
@typeparam TItem where TItem : class, new()

<div class="table">
    <Table TItem="TItem"
           DataSource="Model.Result.PageData"
           Total="Model.Result.TotalCount"
           @bind-PageIndex="Model.Criteria.PageIndex"
           @bind-PageSize="Model.Criteria.PageSize"
           @bind-SelectedRows="Model.SelectedRows"
           OnChange="OnChange"
           HidePagination="!Model.ShowPager"
           PaginationPosition="bottomRight"
           Size="TableSize.Small">
        <ChildContent>
            @if (Model.ShowCheckBox)
            {
                <Selection Width="50" />
            }
            <AutoGenerateColumns Table="Model" Item="context" />
            @if (Model.Actions != null && Model.Actions.Count > 0)
            {
                <ActionColumn Title="操作" Fixed="true" Width="120">
                    @if (Model.Actions.Count > 3)
                    {
                        var first = Model.Actions[0];
                        <Tag Color="@GetActionColor(first.Style)" OnClick="e=>Model.OnAction?.Invoke(first, context)">@first.Name</Tag>
                        <Dropdown>
                            <Overlay>
                                <Menu>
                                    @for (int i = 1; i < Model.Actions.Count; i++)
                                    {
                                        var item = Model.Actions[i];
                                        <MenuItem @key="@item.Id" OnClick="e=>Model.OnAction?.Invoke(item, context)">@item.Name</MenuItem>
                                    }
                                </Menu>
                            </Overlay>
                            <ChildContent>
                                <a class="ant-dropdown-link" @onclick:preventDefault>更多<Icon Type="down" /></a>
                            </ChildContent>
                        </Dropdown>
                    }
                    else
                    {
                        foreach (var item in Model.Actions)
                        {
                            <Tag Color="@GetActionColor(item.Style)" OnClick="e=>Model.OnAction?.Invoke(item, context)">@item.Name</Tag>
                        }
                    }
                </ActionColumn>
            }
        </ChildContent>
        <PaginationTemplate>
            <Pagination Class="@context.PaginationClass"
                        Total="context.Total"
                        PageSize="context.PageSize"
                        Current="context.PageIndex"
                        ShowTotal="showTotal"
                        ShowSizeChanger ShowQuickJumper
                        OnChange="context.HandlePageChange" />
        </PaginationTemplate>
     </Table>
</div>

 @code {
    private string GetActionColor(string style) => style == "danger" ? "red-inverse" : "blue-inverse";
    private Func<PaginationTotalContext, string> showTotal = ctx => $"共 {ctx.Total} 条记录";

    [Parameter] public TableModel<TItem> Model { get; set; }

    protected override void OnInitialized()
    {
        Model.OnRefresh = RefreshAsync;
        base.OnInitialized();
    }

    public async Task RefreshAsync()
    {
        Model.Result = await Model.OnQuery?.Invoke(Model.Criteria);
        StateHasChanged();
    }

    private async Task OnChange(QueryModel<TItem> queryModel)
    {
        if (Model.OnQuery == null)
            return;

        //页码
        Model.Criteria.PageIndex = queryModel.PageIndex;
        Model.Criteria.PageSize = queryModel.PageSize;
        //TODO：表格筛选
        // Model.Criteria.Query.Clear();
        // foreach (var item in queryModel.FilterModel)
        // {
        //     foreach (var filter in item.Filters)
        //     {
        //         var type = GetQueryType(filter.FilterCompareOperator);
        //         Model.Criteria.SetQuery(item.FieldName, type, $"{filter.Value}");
        //     }
        // }
        //排序
        var sorts = queryModel.SortModel.Where(s => !string.IsNullOrWhiteSpace(s.Sort));
        Model.Criteria.OrderBys = sorts.Select(m => GetOrderBy(m)).ToArray();
        Model.Result = await Model.OnQuery?.Invoke(Model.Criteria);
    }

    private QueryType GetQueryType(TableFilterCompareOperator tableFilter)
    {
        switch (tableFilter)
        {
            case TableFilterCompareOperator.Equals:
                return QueryType.Equal;
            case TableFilterCompareOperator.Contains:
                return QueryType.Contain;
            case TableFilterCompareOperator.StartsWith:
                return QueryType.StartWith;
            case TableFilterCompareOperator.EndsWith:
                return QueryType.EndWith;
            case TableFilterCompareOperator.GreaterThan:
                return QueryType.GreatThan;
            case TableFilterCompareOperator.LessThan:
                return QueryType.LessThan;
            case TableFilterCompareOperator.GreaterThanOrEquals:
                return QueryType.GreatEqual;
            case TableFilterCompareOperator.LessThanOrEquals:
                return QueryType.LessEqual;
            case TableFilterCompareOperator.Condition:
                return QueryType.Batch;
            case TableFilterCompareOperator.NotEquals:
                return QueryType.NotEqual;
            case TableFilterCompareOperator.IsNull:
                return QueryType.Equal;
            case TableFilterCompareOperator.IsNotNull:
                return QueryType.NotEqual;
            case TableFilterCompareOperator.NotContains:
                return QueryType.NotEqual;
            case TableFilterCompareOperator.TheSameDateWith:
                return QueryType.Between;
            default:
                return QueryType.Contain;
        }
    }

    private string GetOrderBy(ITableSortModel model)
    {
        //descend  ascend
        var sort = model.Sort == "descend" ? "desc" : "asc";
        return $"{model.FieldName} {sort}";
    }
}